// 震动测试功能
import VibrateDebug from './vibrateDebug.js';

class VibrateTest {
  
  // 延迟函数
  delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  // 测试uni.vibrate
  async testUniVibrate() {
    VibrateDebug.log('测试 uni.vibrate...', 'info');
    return new Promise((resolve) => {
      uni.vibrate({
        success: () => {
          VibrateDebug.log('✅ uni.vibrate 成功', 'success');
          resolve(true);
        },
        fail: (err) => {
          VibrateDebug.log(`❌ uni.vibrate 失败: ${JSON.stringify(err)}`, 'error');
          resolve(false);
        }
      });
    });
  }

  // 测试uni.vibrateLong
  async testUniVibrateLong() {
    VibrateDebug.log('测试 uni.vibrateLong...', 'info');
    return new Promise((resolve) => {
      try {
        uni.vibrateLong({
          success: () => {
            VibrateDebug.log('✅ uni.vibrateLong 成功', 'success');
            resolve(true);
          },
          fail: (err) => {
            VibrateDebug.log(`❌ uni.vibrateLong 失败: ${JSON.stringify(err)}`, 'error');
            resolve(false);
          }
        });
      } catch (error) {
        VibrateDebug.log(`❌ uni.vibrateLong 异常: ${error.message}`, 'error');
        resolve(false);
      }
    });
  }

  // 测试uni.vibrateShort
  async testUniVibrateShort() {
    VibrateDebug.log('测试 uni.vibrateShort...', 'info');
    return new Promise((resolve) => {
      try {
        uni.vibrateShort({
          success: () => {
            VibrateDebug.log('✅ uni.vibrateShort 成功', 'success');
            resolve(true);
          },
          fail: (err) => {
            VibrateDebug.log(`❌ uni.vibrateShort 失败: ${JSON.stringify(err)}`, 'error');
            resolve(false);
          }
        });
      } catch (error) {
        VibrateDebug.log(`❌ uni.vibrateShort 异常: ${error.message}`, 'error');
        resolve(false);
      }
    });
  }

  // 测试原生震动
  async testNativeVibrate() {
    VibrateDebug.log('测试原生震动...', 'info');
    
    // #ifdef APP-PLUS
    try {
      const main = plus.android.runtimeMainActivity();
      const Context = plus.android.importClass('android.content.Context');
      const Vibrator = plus.android.importClass('android.os.Vibrator');
      const Build = plus.android.importClass('android.os.Build');

      const vibrator = main.getSystemService(Context.VIBRATOR_SERVICE);

      if (!vibrator || !vibrator.hasVibrator()) {
        VibrateDebug.log('❌ 设备不支持震动', 'error');
        return false;
      }

      // 根据API版本选择震动方式
      if (Build.VERSION.SDK_INT >= 26) {
        const VibrationEffect = plus.android.importClass('android.os.VibrationEffect');
        const effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
        vibrator.vibrate(effect);
        VibrateDebug.log('✅ 新版原生震动成功', 'success');
      } else {
        vibrator.vibrate(1000);
        VibrateDebug.log('✅ 旧版原生震动成功', 'success');
      }
      return true;
    } catch (error) {
      VibrateDebug.log(`❌ 原生震动失败: ${error.message}`, 'error');
      return false;
    }
    // #endif

    return false;
  }

  // 逐步测试所有震动功能
  async stepByStepTest() {
    VibrateDebug.log('=== 逐步震动测试 ===', 'info');

    await this.testUniVibrate();
    await this.delay(2000);

    await this.testUniVibrateLong();
    await this.delay(2000);

    await this.testUniVibrateShort();
    await this.delay(2000);

    await this.testNativeVibrate();
  }

  // 获取完整诊断报告
  async getFullDiagnostic() {
    VibrateDebug.clearLogs();
    
    await VibrateDebug.detectDeviceInfo();
    await VibrateDebug.checkPermissions();
    await this.stepByStepTest();
    
    return VibrateDebug.getLogs();
  }
}

export default new VibrateTest();